
package org.openeai.lite.common.control;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.openeai.lite.common.exception.OpenEAIException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class ConfigController implements ApplicationContextAware
{

    private static Logger log = Logger.getLogger(ConfigController.class);

    private static Properties properties;

    private static boolean propertiesLoaded = false;
    
    ApplicationContext applicationContext = null;
    
    // run controller as singleton
    private static volatile ConfigController instance;

    private static final Object instanceLock = new Object();

    private ConfigController()
    {
    };

    /**
     * Get the {@link ConfigController} singleton instance in a thread safe way (Java 1.5+ !!!)
     * 
     * @return The {@link ConfigController} singleton instance.
     * @throws OpenEAIException Custom exception
     */
    public static ConfigController getInstance() 
    {
        if (instance == null)
        {
            synchronized (instanceLock)
            {
                if (instance == null)
                {
                    instance = new ConfigController();
                    try {
						loadProperties();
					} 
                    catch (OpenEAIException e) {
						e.printStackTrace();
						return null;
					}
                }
            }
        }

        return instance;
    }

    public Properties getProps()
    {
        try
        {
            if (!propertiesLoaded)
            {
                loadProperties();
            }
            return properties;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public String getProperty(String property)
    {
        return properties.getProperty(property);
    }

    
    private static void loadProperties() throws OpenEAIException
    {
        String propsFile = "openeai.properties";
        properties = new Properties();

        try
        {
            log.info("Loading configuration from properties file (" +propsFile +")");        	
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream inStream = classLoader.getResourceAsStream(propsFile);
            if (inStream != null)
            {
                log.info("Loading properties.");
                properties.load(inStream);
                inStream.close();
            }
            else
            {
                throw new OpenEAIException("Error reading properties file using inputstream!");
            }

            if (!properties.isEmpty())
            {
                log.info("Properties loaded: " + properties.values().size());
                propertiesLoaded = true;
            }
            else
            {
                propertiesLoaded = false;
                log.error("Properties not loaded!");
                throw new OpenEAIException("Error reading system properties");
            }

        }
        catch (Exception e)
        {
            propertiesLoaded = false;
            log.error("Error loading properties file: ", e);
            throw new OpenEAIException("Error loading properties file", e);
        }
    }

    @Override
    public void setApplicationContext(final ApplicationContext aApplicationContext) throws BeansException
    {
        log.info("Setting application context.");
        this.applicationContext = aApplicationContext;
        
    }
    
    public ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }

}
